import 'package:flutter/material.dart';

class OnlinePayState extends ChangeNotifier {
  // 个人类型数据
  Map<String, dynamic> person = {
    "accountType": "1", // 账户类型 1:个人 2:企业
    "accountName": "", // 商户简称
    "contactPhone": "", // 联系手机
    "contactEmail": "", // 联系邮箱
    "idCardFrontImg": "", // 身份证人像面照片
    "idCardBackImg": "", // 身份证国徽面照片
    "idCardNo": "", // 身份证号
    "accountHolder": "", // 姓名
    "idCardStartDate": null, // 身份证有效期开始时间
    "idCardEndDate": null, // 身份证有效期结束时间
    "isIdCardLongTerm": false, // 结束时间是否为长期
    "businessPlaceImg": "", // 经营场所照片
    "bankAccount": "", // 银行账户
    "bankName": "", // 开户银行
  };

  // 企业类型数据
  Map<String, dynamic> company = {
    "accountType": "2", // 账户类型 1:个人 2:企业
    "accountName": "", // 商户简称
    "contactPhone": "", // 联系手机
    "contactEmail": "", // 联系邮箱
    "legalPersonIdCardFrontImg": "", // 身份证人像面照片
    "legalPersonIdCardBackImg": "", // 身份证国徽面照片
    "legalPersonIdCardNo": "", // 身份证号
    "legalPersonName": "", // 姓名
    "legalPersonIdCardStartDate": null, // 身份证有效期开始时间
    "legalPersonIdCardEndDate": null, // 身份证有效期结束时间
    "isIdCardLongTerm": false, // 结束时间是否为长期
    "businessPlaceImg": "", // 经营场所照片
    "bankAccount": "", // 银行账户
    "bankName": "", // 开户银行
    "settlementType": "", // 结算类型
    "businessLicenseImg": "", // 开户许可证照片
    "accountOpeningLicenseImg": "", // 营业执照照片
    "companyName": "", // 商户名称
    "unifiedSocialCreditCode": "", // 统一社会信用代码
    "businessLicenseStartDate": null, // 营业执照有效期开始时间
    "businessLicenseEndDate": null, // 营业执照有效期结束时间
    "isBusinessLicenseLongTerm": false, // 营业执照有效期是否长期有效
    "businessLicenseAddress": "", // 注册地址
  };

  // 个人类型数据更新方法
  void updatePersonBasicInfo({
    String? accountName,
    String? contactPhone,
    String? contactEmail,
  }) {
    if (accountName != null) person["accountName"] = accountName;
    if (contactPhone != null) person["contactPhone"] = contactPhone;
    if (contactEmail != null) person["contactEmail"] = contactEmail;
    notifyListeners();
  }

  void updatePersonIdCardInfo({
    String? idCardFrontImg,
    String? idCardBackImg,
    String? idCardNo,
    String? accountHolder,
    String? idCardStartDate,
    String? idCardEndDate,
    bool? isIdCardLongTerm,
  }) {
    if (idCardFrontImg != null) person["idCardFrontImg"] = idCardFrontImg;
    if (idCardBackImg != null) person["idCardBackImg"] = idCardBackImg;
    if (idCardNo != null) person["idCardNo"] = idCardNo;
    if (accountHolder != null) person["accountHolder"] = accountHolder;
    if (idCardStartDate != null) person["idCardStartDate"] = idCardStartDate;
    if (idCardEndDate != null) person["idCardEndDate"] = idCardEndDate;
    if (isIdCardLongTerm != null) person["isIdCardLongTerm"] = isIdCardLongTerm;
    notifyListeners();
  }

  void updatePersonBusinessPlaceInfo({
    String? businessPlaceImg,
  }) {
    if (businessPlaceImg != null) person["businessPlaceImg"] = businessPlaceImg;
    notifyListeners();
  }

  void updatePersonSettlementInfo({
    String? bankName,
    String? bankAccount,
  }) {
    if (bankName != null) person["bankName"] = bankName;
    if (bankAccount != null) person["bankAccount"] = bankAccount;
    notifyListeners();
  }

  // 企业类型数据更新方法
  void updateCompanyBasicInfo({
    String? accountName,
    String? contactPhone,
    String? contactEmail,
  }) {
    if (accountName != null) company["accountName"] = accountName;
    if (contactPhone != null) company["contactPhone"] = contactPhone;
    if (contactEmail != null) company["contactEmail"] = contactEmail;
    notifyListeners();
  }

  void updateCompanyIdCardInfo({
    String? legalPersonIdCardFrontImg,
    String? legalPersonIdCardBackImg,
    String? legalPersonIdCardNo,
    String? legalPersonName,
    String? legalPersonIdCardStartDate,
    String? legalPersonIdCardEndDate,
    bool? isIdCardLongTerm,
  }) {
    if (legalPersonIdCardFrontImg != null) company["legalPersonIdCardFrontImg"] = legalPersonIdCardFrontImg;
    if (legalPersonIdCardBackImg != null) company["legalPersonIdCardBackImg"] = legalPersonIdCardBackImg;
    if (legalPersonIdCardNo != null) company["legalPersonIdCardNo"] = legalPersonIdCardNo;
    if (legalPersonName != null) company["legalPersonName"] = legalPersonName;
    if (legalPersonIdCardStartDate != null) company["legalPersonIdCardStartDate"] = legalPersonIdCardStartDate;
    if (legalPersonIdCardEndDate != null) company["legalPersonIdCardEndDate"] = legalPersonIdCardEndDate;
    if (isIdCardLongTerm != null) company["isIdCardLongTerm"] = isIdCardLongTerm;
    notifyListeners();
  }

  void updateCompanyBusinessLicenseInfo({
    String? businessLicenseImg,
    String? businessLicenseAddress,
    String? companyName,
    String? businessLicenseStartDate,
    String? businessLicenseEndDate,
    bool? isBusinessLicenseLongTerm,
    String? unifiedSocialCreditCode,
  }) {
    if (businessLicenseImg != null) company["businessLicenseImg"] = businessLicenseImg;
    if (businessLicenseStartDate != null) company["businessLicenseStartDate"] = businessLicenseStartDate;
    if (businessLicenseEndDate != null) company["businessLicenseEndDate"] = businessLicenseEndDate;
    if (isBusinessLicenseLongTerm != null) company["isBusinessLicenseLongTerm"] = isBusinessLicenseLongTerm;
    if (businessLicenseAddress != null) company["businessLicenseAddress"] = businessLicenseAddress;
    if (companyName != null) company["companyName"] = companyName;
    if (unifiedSocialCreditCode != null) company["unifiedSocialCreditCode"] = unifiedSocialCreditCode;
    notifyListeners();
  }

  void updateCompanyAccountLicenseInfo({
    String? accountOpeningLicenseImg,
  }) {
    if (accountOpeningLicenseImg != null) company["accountOpeningLicenseImg"] = accountOpeningLicenseImg;
    notifyListeners();
  }

  void updateCompanyBusinessPlaceInfo({
    String? businessPlaceImg,
  }) {
    if (businessPlaceImg != null) company["businessPlaceImg"] = businessPlaceImg;
    notifyListeners();
  }

  void updateCompanySettlementInfo({
    String? bankName,
    String? bankAccount,
    String? settlementType,
    String? bankProvince, // 开户省
    String? bankCity,
  }) {
    if (bankName != null) company["bankName"] = bankName;
    if (bankAccount != null) company["bankAccount"] = bankAccount;
    if (settlementType != null) company["settlementType"] = settlementType;
    notifyListeners();
  }

  // 个人类型完成状态检查
  bool get isPersonBasicInfoCompleted => 
    person["accountName"].toString().isNotEmpty && 
    person["contactPhone"].toString().isNotEmpty && 
    person["contactEmail"].toString().isNotEmpty;

  bool get isPersonIdCardCompleted => 
    person["idCardFrontImg"].toString().isNotEmpty && 
    person["idCardBackImg"].toString().isNotEmpty && 
    person["idCardNo"].toString().isNotEmpty && 
    person["accountHolder"].toString().isNotEmpty &&
    (person["isIdCardLongTerm"] == true || 
    (person["idCardStartDate"].toString().isNotEmpty && 
     person["idCardEndDate"].toString().isNotEmpty));

  bool get isPersonBusinessPlaceCompleted => 
    person["businessPlaceImg"].toString().isNotEmpty;

  bool get isPersonSettlementCompleted => 
    person["bankName"].toString().isNotEmpty && 
    person["bankAccount"].toString().isNotEmpty;

  // 企业类型完成状态检查
  bool get isCompanyBasicInfoCompleted => 
    company["accountName"].toString().isNotEmpty && 
    company["contactPhone"].toString().isNotEmpty && 
    company["contactEmail"].toString().isNotEmpty;

  bool get isCompanyIdCardCompleted => 
    company["legalPersonIdCardFrontImg"].toString().isNotEmpty && 
    company["legalPersonIdCardBackImg"].toString().isNotEmpty && 
    company["legalPersonIdCardNo"].toString().isNotEmpty && 
    company["legalPersonName"].toString().isNotEmpty &&
    (company["isIdCardLongTerm"] == true || 
    (company["legalPersonIdCardStartDate"].toString().isNotEmpty && 
     company["legalPersonIdCardEndDate"].toString().isNotEmpty));

  bool get isCompanyBusinessLicenseCompleted => 
    company["businessLicenseImg"].toString().isNotEmpty && 
    (company["isBusinessLicenseLongTerm"] == true || 
    (company["businessLicenseStartDate"].toString().isNotEmpty && 
     company["businessLicenseEndDate"].toString().isNotEmpty)) &&
    company["companyName"].toString().isNotEmpty &&
    company["unifiedSocialCreditCode"].toString().isNotEmpty && 
    company["businessLicenseAddress"].toString().isNotEmpty;

  bool get isCompanyAccountLicenseCompleted => 
    company["accountOpeningLicenseImg"].toString().isNotEmpty;

  bool get isCompanyBusinessPlaceCompleted => 
    company["businessPlaceImg"].toString().isNotEmpty;

  bool get isCompanySettlementCompleted => 
    company["bankName"].toString().isNotEmpty && 
    company["bankAccount"].toString().isNotEmpty && 
    company["settlementType"].toString().isNotEmpty;

  // 提交检查
  bool canSubmitPerson(bool isAgreed) {
    if (!isAgreed) return false;
    return isPersonBasicInfoCompleted && 
           isPersonIdCardCompleted && 
           isPersonBusinessPlaceCompleted && 
           isPersonSettlementCompleted;
  }

  bool canSubmitCompany(bool isAgreed) {
    if (!isAgreed) return false;
    return isCompanyBasicInfoCompleted && 
           isCompanyIdCardCompleted && 
           isCompanyBusinessLicenseCompleted && 
           isCompanyAccountLicenseCompleted && 
           isCompanyBusinessPlaceCompleted && 
           isCompanySettlementCompleted;
  }

  // 批量更新个人数据
  void updatePersonData(Map<String, dynamic> data) {
    // 只更新非空值，保留现有的默认结构
    data.forEach((key, value) {
      if (value != null && person.containsKey(key)) {
        person[key] = value;
      }
    });
    notifyListeners();
  }

  // 批量更新企业数据
  void updateCompanyData(Map<String, dynamic> data) {
    // 只更新非空值，保留现有的默认结构
    data.forEach((key, value) {
      if (value != null && company.containsKey(key)) {
        company[key] = value;
      }
    });
    notifyListeners();
  }

  // 从API响应数据更新状态
  void updateFromApiData(Map<String, dynamic> apiData) {
    if (apiData.isEmpty) return;
    
    final accountType = apiData['accountType']?.toString();
    
    if (accountType == '1') {
      // 个人类型数据更新
      updatePersonData(apiData);
    } else if (accountType == '2') {
      // 企业类型数据更新
      updateCompanyData(apiData);
    } else {
      // 如果accountType未知，尝试根据数据字段判断
      if (apiData.containsKey('legalPersonName') || apiData.containsKey('companyName')) {
        // 包含企业特有字段，认为是企业数据
        updateCompanyData(apiData);
      } else {
        // 默认作为个人数据处理
        updatePersonData(apiData);
      }
    }
  }

  // 获取当前账户类型（用于判断是个人还是企业）
  String? getCurrentAccountType(Map<String, dynamic> apiData) {
    return apiData['accountType']?.toString();
  }
} 